﻿IF EXISTS (SELECT * FROM sysobjects WHERE type = 'P' AND name = 'usersGetInRole')
	BEGIN
		DROP  Procedure  usersGetInRole
	END
GO

-- =============================================
-- Author:		Ricardo Sosa
-- Create date: 03/04/2009
-- Description:	Finds the users that match the search criteria and belong to the specified role
-- =============================================
CREATE PROCEDURE [dbo].[usersGetInRole]
	@role_name VARCHAR(50),
	@app_id INT
AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;

	DECLARE @role_id INT;

	SELECT @role_id = lit_rol_id
	FROM lit_rol
	WHERE lit_rol_nombre = @role_name
	AND lit_aplicacion_id = @app_id;
	
	IF (@role_id IS NOT NULL) BEGIN
		SELECT u.* 
		FROM lit_usuario u
		JOIN lit_usuario_rol ur ON u.lit_usuario_id = ur.lit_usuario_id
		WHERE ur.lit_rol_id = @role_id
		ORDER BY lit_usuario_username;
	END
END
GO